package com.lebang.service;

import com.lebang.domain.MemberRebateTotalmoney;
import com.lebang.mapper.MemberRebateTotalmoneyMapper;
import com.lebang.model.Whether;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class MemberRebateTotalmoneyService {
    @Autowired
    private MemberRebateTotalmoneyMapper memberRebateTotalmoneyMapper;


    public MemberRebateTotalmoney selectById(String memberId) {
        //进来先查询是否有当前用户的返利累计表
        MemberRebateTotalmoney memberRebateTotalmoney = memberRebateTotalmoneyMapper.selectById(memberId);
        if (memberRebateTotalmoney==null){
            MemberRebateTotalmoney memberRebateTotalmoney1=new MemberRebateTotalmoney();
            memberRebateTotalmoney1.setCreateTime(LocalDateTime.now());
            memberRebateTotalmoney1.setMemberId(memberId);
            memberRebateTotalmoney1.setTotalMoney(0);
            memberRebateTotalmoney1.setTotalTsMoney(0);
            memberRebateTotalmoney1.setEnable(Whether.YES.getDbIndex());
            memberRebateTotalmoneyMapper.insertSelective(memberRebateTotalmoney1);
            //从新查询并返回
            return memberRebateTotalmoney1;
        }
        return memberRebateTotalmoney;

    }

    public void addTotalMoney(String memberId, int type, int amount) {
        if (type==0){
            //记录返利给师傅的金额累计
            memberRebateTotalmoneyMapper.addTotalMoney(memberId,amount);
        }else if (type==1){
            //记录返利给师傅的师傅金额累计
            memberRebateTotalmoneyMapper.addTotalTsMoney(memberId,amount);
        }
    }
}
